package com.smartleanx.module.hrm.task.job;

import cn.hutool.core.collection.CollUtil;
import com.smartleanx.framework.tenant.core.job.TenantJob;
import com.smartleanx.module.hrm.app.empRisk.EmployeeRiskHandleChainService;
import com.smartleanx.module.hrm.app.empRisk.EmployeeRiskHandleService;
import com.smartleanx.module.hrm.app.risk.RiskHandleChainService;
import com.smartleanx.module.hrm.dal.dataobject.companysystem.CompanySystemDO;
import com.smartleanx.module.hrm.dal.dataobject.employee.EmployeeDO;
import com.smartleanx.module.hrm.service.companysystem.CompanySystemService;
import com.smartleanx.module.hrm.service.employee.EmployeeService;
import com.smartleanx.module.hrm.util.WebhookService;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static com.smartleanx.module.hrm.eums.risk.RiskTypeEnum.LABOR_CONTRACT;
import static com.smartleanx.module.hrm.eums.risk.RiskTypeEnum.LABOR_CONTRACT_EXPIRATION;

/**
 * 风险扫描任务
 *
 * @author tengcj
 * @since 2024/6/24
 */
@Slf4j
@Component
public class RiskScanJob {

    @Resource
    private EmployeeService employeeService;

    @Resource
    private CompanySystemService companySystemService;

    @Resource
    private EmployeeRiskHandleService employeeRiskHandleService;

    @Resource
    private EmployeeRiskHandleChainService employeeRiskHandleChainService;

    @Resource
    private RiskHandleChainService riskHandleChainService;

    @Resource
    private WebhookService webhookService;

    /**
     * 劳动合同扫描任务
     */
    @XxlJob("RiskLaborContractJob")
    @TenantJob
    public void riskLaborContractJob() {
        log.info("合同签订风险扫描任务开始====================");
        List<EmployeeDO> allEmployee = employeeService.getAllEmployee();
        if (CollUtil.isEmpty(allEmployee)) {
            return;
        }
        employeeRiskHandleService.execute(allEmployee.stream().map(EmployeeDO::getId).collect(Collectors.toSet()), LABOR_CONTRACT.getCode());
        log.info("合同签订风险扫描任务结束====================");
    }

    /**
     * 劳动合同续签扫描任务
     */
    @XxlJob("RiskLaborContractRenewalJob")
    @TenantJob
    public void riskLaborContractRenewalJob() {
        log.info("劳动合同续签风险扫描任务开始====================");
        try {
            List<EmployeeDO> allEmployee = employeeService.getAllEmployee();
            if (CollUtil.isEmpty(allEmployee)) {
                return;
            }
            employeeRiskHandleService.execute(allEmployee.stream().map(EmployeeDO::getId).collect(Collectors.toSet()), LABOR_CONTRACT_EXPIRATION.getCode());
        } catch (Exception e) {
            String message = "劳动合同续签风险扫描任务异常, 异常信息：%s";
            webhookService.sendMessage(String.format(message, e.getMessage()));
            throw e;
        }
        log.info("劳动合同续签风险扫描任务结束====================");
    }

    @XxlJob("FullRiskScanJob")
    @TenantJob
    public void fullRiskScanJob() {
        log.info("全部风险扫描任务开始====================");

        List<EmployeeDO> allEmployee = employeeService.getAllEmployee();
        if (CollUtil.isNotEmpty(allEmployee)) {
            log.info("员工风险扫描====================");
            Set<Long> employeeIds = allEmployee.stream().map(EmployeeDO::getId).collect(Collectors.toSet());
            employeeRiskHandleChainService.execute(employeeIds);
        }

        List<CompanySystemDO> allCompanySystem = companySystemService.getAllCompanySystem();
        if (CollUtil.isNotEmpty(allCompanySystem)) {
            log.info("制度风险扫描====================");
            Set<Long> recordIds = allCompanySystem.stream().map(CompanySystemDO::getId).collect(Collectors.toSet());
            riskHandleChainService.execute(recordIds);
        }

        log.info("全部风险扫描任务结束====================");
    }

}
